@extends('layouts.app')
@section('title','用户收货地址列表页')

@section('content')
    <div class="row">
        <div class="col-md-10 offset-md-1">
            <div class="card">
                <div class="card-header">
                    用户收货地址列表
                    <a class="float-right" href="{{route('user_addresses.create')}}">新建收货地址</a>
                </div>
                <div class="card-body">
                    <table class="table table-striped table-bordered">
                        <thead>
                        <tr>
                            <th scope="col">#</th>
                            <th scope="col">收货人</th>
                            <th scope="col">地址</th>
                            <th scope="col">邮编</th>
                            <th scope="col">电话</th>
                            <th scope="col">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($addresses as $k=>$address)
                        <tr>
                            <td>{{$k+1}}</td>
                            <td>{{$address->contact_name}}</td>
                            <td>{{$address->full_address}}</td>
                            <td>{{$address->zip}}</td>
                            <td>{{$address->contact_phone}}</td>
                            <td>
                                <a class="btn btn-sm btn-primary"
                                href="{{route('user_addresses.edit',['user_address'=>$address->id])}}"
                                >编辑</a>
                                <button class="btn btn-danger btn-sm btn-del-address" data-id="{{$address->id}}">删除</button>
                            </td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@stop

@section('scriptsAfterJs')
    <script>
        $(document).ready(function (){
            $('.btn-del-address').click(function(){
                let id = $(this).data('id')
                swal({
                    title:'确认要删除该地址吗？',
                    icon:'warning',
                    buttons:['取消','确认'],
                    // 默认焦点在取消按钮上
                    dangerMode:true
                }).then(function (willDelete){
                    if(!willDelete){
                        return
                    }
                    axios.delete('/user_addresses/'+id).then(function (){
                        location.reload()
                    })
                })
            })
        })
    </script>
@stop
